package server.test

import org.scalatest._
import server.world._
import server.world.item._
import server.world.action._

import server.world.map._
import server.world.character._



class TPSuite extends Suite with OneInstancePerTest{

TurnProcessor.flush()
  
val testloc = CTE.makeTestMap(5, 5)


val npc1 = new NPC(testloc.tileList(1)) 
val npc2 = new NPC(testloc.tileList(3))
val npc3 = new NPC(testloc.tileList(5))
val npc4 = new NPC(testloc.tileList(7))

npc1.skillbook.addSkill("TEST")
npc2.skillbook.addSkill("TEST")
npc3.skillbook.addSkill("TEST")
npc4.skillbook.addSkill("TEST")

val act1 = new TestAction(npc1,4)
val act2 = new TestAction(npc2,3)
val act3 = new TestAction(npc3,10)
val act4 = new TestAction(npc4,2)

class TestAction(val char:GameCharacter, var ap:Float) extends Action{
  override val skillname = "TEST"
  var condition = true
  override def predicate() = condition
  override def exe(){
    
  }
}

def testAddMethods() {
AI.addNPC(npc1)
TurnProcessor.addAction(new TestAction(npc1, 2))

assert(AI.NPCList.contains(npc1), "NPCList length: " + AI.NPCList.length.toString())
assert(!TurnProcessor.getActionStack.isEmpty, "ActionStack length: " + TurnProcessor.getActionStack.length.toString())
}

def testProcessTurnSortFunction(){ // Test that actions are sorted according to their AP cost

TurnProcessor.addAction(act1)
TurnProcessor.addAction(act2)
TurnProcessor.addAction(act3)
TurnProcessor.addAction(act4)
TurnProcessor.processTurn

var actList = TurnProcessor.getActionStack()
val assertpred = {(x:Action) => if(actList.indexOf(x)+1 == actList.length){true} else
                                x.ap <= actList.apply(actList.indexOf(x) + 1).ap
}

assert(actList.forall {assertpred})
}


def testProcessTurnPop(){ // Test that 0 AP actions are poped from stack
TurnProcessor.addAction(act4)
TurnProcessor.processTurn
TurnProcessor.processTurn
val actList = TurnProcessor.getActionStack()
assert(actList.isEmpty)
}

def testRemoveAction(){  
  TurnProcessor.addAction(act1)
  val stack = TurnProcessor.getActionStack()
  TurnProcessor.removeAction(stack(0))
  
  assert(!TurnProcessor.getActionStack().contains(stack(0)))
}


def testRemoveCharacterAction(){
  TurnProcessor.addAction(act1)
  TurnProcessor.removeCharacterAction(npc1)
  
  assert(!TurnProcessor.getActionStack().contains(act1))
}

def testCheckPredicate(){
  act1.condition = false
  TurnProcessor.addAction(act1)
  TurnProcessor.addAction(act2)
  TurnProcessor.processTurn
  assert(!TurnProcessor.getActionStack().contains(act1) & TurnProcessor.getActionStack().contains(act2))
}

def testDuplicateActionSubmition(){
  TurnProcessor.addAction(act1)
  TurnProcessor.addAction(act1)
  assert(TurnProcessor.getActionStack().length == 1)
}

def testTilebuilder(){ // check if passing container Tile as an argument for Tile is all right
  assert(testloc.tileList(0).map == testloc.tileList(7).map)
  assert(testloc.tileList(0).map.tileList.length == testloc.tileList(7).map.tileList.length)
}

}